//////////////////////////////////////////////////////////////////////
////                                                              ////
////  FlashWaitTimer.v                                            ////
////                                                              ////
////                                                              ////
////  This file is part of the "Pice E12" project                 ////
////  http://www.picocomputing.com                                ////
////                                                              ////
////                                                              ////
//////////////////////////////////////////////////////////////////////
////                                                              ////
//// Copyright (C) 2005, Pico Computing, INC.                     ////
//// http://www.picocomputing.com                                 ////
////                                                              ////
//// This source file may be used and distributed without         ////
//// restriction provided that this copyright statement is not    ////
//// removed from the file and that any derivative work contains  ////
//// the original copyright notice and the associated disclaimer. ////
////                                                              ////
//// This source file is free software; you can redistribute it   ////
//// and/or modify it under the terms of the GNU Lesser General   ////
//// Public License as published by the Free Software Foundation; ////
//// either version 2.1 of the License, or (at your option) any   ////
//// later version.                                               ////
////                                                              ////
//// This source is distributed in the hope that it will be       ////
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
//// PURPOSE.  See the GNU Lesser General Public License for more ////
//// details.                                                     ////
////                                                              ////
//// You should have received a copy of the GNU Lesser General    ////
//// Public License along with this source; if not, download it   ////
//// from http://www.opencores.org/lgpl.shtml                     ////
////                                                              ////
////////////////////////////////////////////////////////////////////JF

//This module control the Wait line to extend a read or write cycle if the host is going faster than 
//the seek time on the Flash FLASH.

//The minimum PCMCIA read cycle is 100nS	 
`define FLASH_WAIT_CYCLES 24	//units = 5 nSec

module FlashWaitTimer(CFClock, FLASH_OE, FLASH_CE, Wait);

input CFClock;                                                  //200MHz free running CFClock
output Wait;                                                    //PCMCIA extend cycle request
input FLASH_OE;                                                 //FLASH Output Enable
input FLASH_CE;

wire BeginWait;
reg LastBeginWait;
reg [`FLASH_WAIT_CYCLES-1:0]Shifter;                            //240 nS Delay Register
//synthesis attribute init LastBeginWait 0;
//synthesis attribute init Shifter 0;

assign BeginWait = (~FLASH_OE) && (~FLASH_CE);						 //Begin wait whenever there is a valid read transaction

always @(posedge CFClock) begin
   if (BeginWait & ~LastBeginWait)										 //Fill Shifter when we first see our read
		Shifter[`FLASH_WAIT_CYCLES-1:0] <= {`FLASH_WAIT_CYCLES{1'b1}};              
	else																			 //Let it shift otherwise
	   Shifter[`FLASH_WAIT_CYCLES-1:0] <= {Shifter[`FLASH_WAIT_CYCLES-2:0], 1'b0}; 
	LastBeginWait <= BeginWait;
end

assign Wait = Shifter[`FLASH_WAIT_CYCLES-1];

endmodule